<?php
namespace Tests\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Mockery;
use Mockery\Mock;
use stdClass;

/**
 *
 * Author: ZeonWang
 * Date: 2018/1/24
 * Time: 11:45
 * Created by PanxSoft.
 */

trait Mockable {

    /**
     * @example
     * 在 Unit Test 中先调用
     *      $this->mockGuardUser('admin', $admin);
     * 代码中的
     *      Auth::Guard('admin')->user() 会返回 $admin
     *
     * @param string $guard
     * @param $user
     */
    public function mockGuardUser($guard, Model $user)
    {
        /** @var Mock $mock_guard */
        $mock_guard = Mockery::mock(stdClass::class);

        Auth::shouldReceive('guard')
            ->with($guard)
            ->andReturn($mock_guard);

        $mock_guard->shouldReceive('user')->andReturn($user);
    }
}